<template>
  <div class="app-container">
    <div
      style="
        display: flex;
        margin-left: 20px;
        margin-top: 20px;
        align-items: center;
      "
    >
      <div>
        <!-- 每日统计 -->
        <el-date-picker
          v-model="timeValue"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="YYYY-MM-DD"
          style="width: 300px; margin: 0 12px 10px 0"
          @change="handleTime"
        />
      </div>

      <div
        style="
          width: 210px;
          display: flex;
          align-items: center;
          justify-content: left;
          font-size: 14px;
          margin-top: -10px;
        "
      >
        <span>部门/人员:</span>
        <select-show
          v-model:orgList="deptOrUserList"
          type="org"
          :disable-select-children-dept="false"
          :multiple="true"
          :showStyle="3"
        ></select-show>
      </div>

      <el-checkbox
        v-model="containCheck"
        style="margin-left: 20px"
        @change="handleContain"
      >
        离职员工
      </el-checkbox>
      <span style="font-size: 12px; color: #777">(保留三个月)</span>
    </div>
    <!-- 导出按钮 -->
    <!-- <el-row style="margin-bottom: 20px; margin-top: 10px; margin-left: 20px">
      <el-col>
        <el-button type="primary" plain icon="Download" @click="handleExport"
          >导出</el-button
        >
      </el-col>
    </el-row> -->
    <el-table v-loading="loading" :data="postList">
      <el-table-column label="姓名" align="center" prop="userName" />
      <el-table-column label="考勤组" align="center" prop="groupName" />
      <el-table-column label="部门" align="center" prop="dept" />
      <el-table-column label="工号" align="center" prop="jobId" />
      <el-table-column label="职位" align="center" prop="postName" />
      <el-table-column label="考勤日期" align="center" prop="attendDay" />
      <el-table-column label="班次" align="center" prop="attendShift" />

      <el-table-column label="上班1" align="center">
        <el-table-column label="打卡时间" align="center" prop="officeTime" />
        <el-table-column label="打卡结果" align="center" prop="officeResult" />
      </el-table-column>
      <el-table-column label="下班1" align="center">
        <el-table-column label="打卡时间" align="center" prop="closingTime" />
        <el-table-column label="打卡结果" align="center" prop="closingResult" />
      </el-table-column>
      <el-table-column label="上班2" align="center">
        <el-table-column label="打卡时间" align="center" prop="officeTimeTwo" />
        <el-table-column
          label="打卡结果"
          align="center"
          prop="officeResultTwo"
        />
      </el-table-column>
      <el-table-column label="下班2" align="center">
        <el-table-column
          label="打卡时间"
          align="center"
          prop="closingTimeTwo"
        />
        <el-table-column
          label="打卡结果"
          align="center"
          prop="closingResultTwo"
        />
      </el-table-column>

      <el-table-column label="关联的审批单" align="center" prop="flowId" />
      <el-table-column label="出勤天数" align="center" prop="attendDayNumber" />
      <el-table-column label="出勤班次" align="center" prop="attendShiftid;" />
      <el-table-column label="休息天数" align="center" prop="restDays" />
      <el-table-column label="工作时长(分钟)" align="center" prop="workHours" />
      <el-table-column label="迟到次数" align="center" prop="lateNumber" />
      <el-table-column label="迟到时长(分钟)" align="center" prop="lateHours" />
      <el-table-column
        label="严重迟到次数"
        align="center"
        prop="seriousLateNumber"
      />
      <el-table-column
        label="严重迟到时长(分钟)"
        align="center"
        prop="seriousLateHours"
      />
      <el-table-column label="旷工迟到天数" align="center" prop="lateDays" />
      <el-table-column
        label="早退次数"
        align="center"
        prop="leaveEarlyNumber"
      />
      <el-table-column
        label="早退时长(分钟)"
        align="center"
        prop="leaveEarlyHours"
      />
      <el-table-column label="缺卡次数" align="center" prop="notClockNumber" />
      <el-table-column label="旷工天数" align="center" prop="absentDays" />
      <el-table-column label="外出（小时）" align="center" prop="outHours" />
      <el-table-column label="加班时长（转调休）" align="center">
        <el-table-column
          label="工作日（转调休）（小时）"
          align="center"
          prop="workOvertimeHours"
        />
        <el-table-column
          label="休息日（转调休）（小时）"
          align="center"
          prop="restovertimeHours"
        />
        <el-table-column
          label="节假日（转调休）（小时）"
          align="center"
          prop="holidayovertimeHours"
        />
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script setup name="Daily">
import { getDailyListApi } from "@/api/attendance/statistics";
import selectShow from "@/views/flyflow/components/orgselect/selectAndShow.vue";
import moment from "moment";

const { proxy } = getCurrentInstance();
const postList = ref([]);
const loading = ref(true);
const total = ref(0);
const timeValue = ref([
  moment().startOf("month").format("YYYY-MM-DD"),
  moment().format("YYYY-MM-DD"),
]);
const deptOrUserList = ref([]);
const containCheck = ref(true);

const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    startDay: moment().startOf("month").format("YYYY-MM-DD"), //开始日期
    endDay: moment().format("YYYY-MM-DD"), //结束日期
    deptIds: [], //部门集合
    userIds: [], //员工集合
    contain: 2, //是否包含90天离职人员 1:不包含 2:包含
  },
});

/**
 * 监听部门人员查询
 */
watch(
  () => deptOrUserList.value,
  (val) => {
    queryParams.value.userIds = [];
    queryParams.value.deptIds = [];
    val.map((item) => {
      if (item.type == "user") {
        queryParams.value.userIds.push(item.id);
      } else if (item.type == "dept") {
        queryParams.value.deptIds.push(item.id);
      }
    });

    getList();
  },
  { deep: true }
);

const { queryParams } = toRefs(data);

/** 查询每日统计列表 */
function getList() {
  loading.value = true;
  getDailyListApi(queryParams.value).then((response) => {
    postList.value = response.rows;
    total.value = Number(response.total);
    loading.value = false;
  });
}

/**
 * 处理时间查询
 */
const handleTime = (val) => {
  queryParams.value.startDay = val[0];
  queryParams.value.endDay = val[1];
  getList();
};

/**
 * 处理是否90天内离职人员查询
 */
const handleContain = () => {
  containCheck.value == true
    ? (queryParams.value.contain = 2)
    : (queryParams.value.contain = 1);
  getList();
};

/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    "system/post/export",
    {
      ...queryParams.value,
    },
    `post_${new Date().getTime()}.xlsx`
  );
}

getList();
</script>
